var express = require('express');
var router = express.Router();
var retUtil = require('../../../../utils/retUtil');
var api4back = require('../../../../service/api4back');
var CONST = require('../../../../utils/const');


/**
 * 获取合作方(网红公司)参与的活动列表
 * @author 李强
 * @date 2018-07-9
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：withRewardInfo : 是否返回活动奖励信息
 * 参数：coopOrgId : 合作方组织id
 * 参数：title : 检索条件：活动标题
 * 参数：findStates : 检索条件：活动状态
 */
function getCoopActInfoList(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10000;
    var withTotalPage = 1;
    var withRewardInfo = 1;
    var coopOrgId = 0;
    var findStates = retUtil.stringFromReq(req, 'findStates') || "";
    var title = retUtil.stringFromReq(req, 'title') || "";

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].orgInfo[0] && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        coopOrgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    }else{
        return retUtil.ret_err_json("用户组织无效",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            withRewardInfo:withRewardInfo,
            coopOrgId:coopOrgId
        },
        method : "POST"
    };

    if(findStates.length && findStates != "null"){
        para.body.findStates = findStates;
    }
    if(title.length && title != "null"){
        para.body.title = title;
    }
    api4back.getCoopActInfoList(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                if(apiRes.list[i].valid_state == 10){
                    apiRes.list[i].state_desc = "未开场";
                }else if(apiRes.list[i].valid_state == 20){
                    apiRes.list[i].state_desc = "已开场";
                }else if(apiRes.list[i].valid_state == 30){
                    apiRes.list[i].state_desc = "已截止";
                }else if(apiRes.list[i].valid_state == 40){
                    apiRes.list[i].state_desc = "已结束";
                }
            }
        };
        var para1 = {
            body :{
                orgId:coopOrgId
            },
            method : "POST"
        };
        api4back.getOrgActStatsInfo(para1,function (postErr1, apiRes1) {
            if(postErr1) {
                return retUtil.ret_err_json(postErr1, res);
            }
            apiRes.companyInfo = apiRes1;
            return retUtil.ret_succ_json(apiRes, res);
        })
    });
}
router.get('/getCoopActInfoList', getCoopActInfoList);
router.post('/getCoopActInfoList', getCoopActInfoList);




/**
 * 获取合作方某个活动下参与的作品列表
 * @author 李强
 * @date 2018-07-9
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：withRewardInfo : 是否返回活动奖励信息
 * 参数：coopOrgId : 合作方组织id
 * 参数：title : 检索条件：活动标题
 * 参数：findStates : 检索条件：活动状态
 */
function getActUserProdInfoList(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10;
    var withTotalPage = 1;
    var orgId = retUtil.numberFromReq(req, 'orgId') || 0;
    var actId = retUtil.numberFromReq(req, 'actId') || 0;
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    var mobile = retUtil.numberFromReq(req, 'mobile') || 0;
    var findStates = retUtil.stringFromReq(req, 'findStates') || "";

    if(!coopOrgId){
        return retUtil.ret_err_json("参数错误",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage
        },
        method : "POST"
    };

    if(findStates.length && findStates != "null"){
        para.body.findStates = findStates;
    }
    if(orgId && orgId != "null"){
        para.body.orgId = orgId;
    }
    if(actId && actId != "null"){
        para.body.actId = actId;
    }
    if(userId && userId != "null"){
        para.body.userId = userId;
    }
    if(mobile && mobile != "null"){
        para.body.mobile = mobile;
    }


    api4back.getActUserProdInfoList(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        apiRes.pageSize = pageSize;
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                apiRes.list[i].key = apiRes.list[i].aup_id;
            }
        }

        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getActUserProdInfoList', getActUserProdInfoList);
router.post('/getActUserProdInfoList', getActUserProdInfoList);




/**
 * 获取公司/组织用户列表
 * @author 严旭东
 * @date 2018-07-11
 */
function getYjxOrgUserInfoList(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10;
    var withTotalPage = 1;
    var orgId = 0;
    var mobile = retUtil.stringFromReq(req, 'mobile') || "";
    var nickname = retUtil.stringFromReq(req, 'nickname') || '';
    var userId = retUtil.numberFromReq(req, 'userId')|| 0;

    if(!nickname || nickname.length == 0 || nickname == "null"){
        nickname = "";
    }

    if(!mobile || mobile.length == 0 || mobile == "null"){
        mobile = "";
    }

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].orgInfo[0] && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        orgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    }else{
        return retUtil.ret_err_json("用户组织无效",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            orgId:orgId
        },
        method : "POST"
    };


    if(mobile && mobile.length){
        para.body.mobile = mobile;
    }
    if(userId){
        para.body.userId = userId;
    }
    if(nickname && nickname.length){
        para.body.nickname = nickname;
    }

    api4back.getYjxOrgUserInfoList(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        apiRes.pageSize = pageSize;
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                apiRes.list[i].key =apiRes.list[i].user_id;
            }
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getYjxOrgUserInfoList', getYjxOrgUserInfoList);
router.post('/getYjxOrgUserInfoList', getYjxOrgUserInfoList);


/**
 * 添加合作方(网红公司)成员
 * @author 严旭东
 * @date 2018-07-11
 */
function addYjxCoopUserInfo(req, res, next) {
    //获取参数
    var orgId = 0;
    var mobile = retUtil.numberFromReq(req, 'mobile01') || 0;
    var desc = retUtil.stringFromReq(req, 'desc') || '';
    var verCd = retUtil.numberFromReq(req, 'verCd')|| 0;


    if(!verCd || verCd.length == 0 || !mobile || desc.length == 0){
        return retUtil.ret_err_json("参数错误",res);
    }

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].orgInfo[0] && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        orgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    }else{
        return retUtil.ret_err_json("用户组织无效",res);
    }

    //数据请求
    var para = {
        body :{
            orgId:orgId,
            mobile:mobile,
            desc:desc,
            verCd:verCd
        },
        method : "POST"
    };

    api4back.addYjxCoopUserInfo(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/addYjxCoopUserInfo', addYjxCoopUserInfo);
router.post('/addYjxCoopUserInfo', addYjxCoopUserInfo);



/**
 * 移除合作方(网红公司)成员
 * @author 严旭东
 * @date 2018-07-11
 */
function removeYjxCoopUser(req, res, next) {
    //获取参数
    var orgId = 0;
    var userId = retUtil.numberFromReq(req, 'userId') || 0;

    if(!userId){
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].orgInfo[0] && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        orgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    }else{
        return retUtil.ret_err_json("用户组织无效",res);
    }

    const currentUserId = req.session[CONST.SESSION_USER].userInfo.userId;
    //判断是否为管理员
    if(!currentUserId || currentUserId ===null){
        return retUtil.ret_err_json("页面管理员失效,重新刷新页面",res);
    }

    if (currentUserId === userId){// 如果当前用户ID等于需要删除的用户的ID 则不能删除
        return retUtil.ret_err_json("不能删除管理员自己账号",res);
    }


    //数据请求
    var para = {
        body :{
            orgId:orgId,
            userId:userId
        },
        method : "POST"
    };


    api4back.removeYjxCoopUser(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/removeYjxCoopUser', removeYjxCoopUser);
router.post('/removeYjxCoopUser', removeYjxCoopUser);
//===========================================================================


/**
 * 获取网红公司个人用户资料
 * @author 李琦
 * @date 2018-07-25
 */
function getRedWedUserInfo(req, res, next) {
    //获取参数
    var orgId = 0;
    var userId = retUtil.numberFromReq(req, 'userId') || 0;

    if(!userId){
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //参数判断
    if(req.session[CONST.SESSION_USER] && req.session[CONST.SESSION_USER].orgInfo[0] && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        orgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    }else{
        return retUtil.ret_err_json("用户组织无效",res);
    }

    //判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0){
        return retUtil.ret_err_json("没有权限",res);
    }

    //数据请求
    var para = {
        body :{
            orgId:orgId,
            userId:userId
        },
        method : "POST"
    };


    api4back.getRedWedUserInfo(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getRedWedUserInfo', getRedWedUserInfo);
router.post('/getRedWedUserInfo', getRedWedUserInfo);

/**
 * 编辑网红资料信息
 * @author 李琦
 * @date 2018-07-25
 */
function editWebRedUserInfo(req, res, next) {
    //获取参数
    var orgId = 0;
    var userId = retUtil.numberFromReq(req, 'userId') || 0;

    if(!userId){
        return retUtil.ret_err_json("用户唯一表示无效",res);
    }

    //判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0){
        return retUtil.ret_err_json("没有权限",res);
    }

    //数据请求
    var para = {
        body :{
            orgId:orgId,
            userId:userId
        },
        method : "POST"
    };

    api4back.addYjxCoopUserInfo(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/editWebRedUserInfo', editWebRedUserInfo);
router.post('/editWebRedUserInfo', editWebRedUserInfo);


/**
 * 给网红用户绑定平台资料
 * @author 李琦
 * @date 2018-07-25
 */
function addUserPlat(req, res, next) {
    // 获取参数
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    var plat = retUtil.numberFromReq(req,'plat')||0;// 平台
    var platId = retUtil.stringFromReq(req,'platId')||0;// 平台私有ID 例如:抖音ID
    var fansNum = retUtil.numberFromReq(req,'fansNum')||0;// 粉丝数
    var likeNum = retUtil.numberFromReq(req,'likeNum')||0;// 点赞数

    if(!userId || !plat || !platId ||!fansNum){
        return retUtil.ret_err_json("参数为空",res);
    }

    // 判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0
        ||req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("没有权限",res);
    }

    const currentUserId = req.session[CONST.SESSION_USER].userInfo.userId;

    // 数据请求
    var para = {
        body :{
            userId:userId,
            currentUserId:currentUserId,
            plat:plat,
            platId:platId,
            fansNum:fansNum,
            likeNum:likeNum
        },
        method : "POST"
    };

    api4back.addUserPlat(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/addUserPlat', addUserPlat);
router.post('/addUserPlat', addUserPlat);


/**
 * author:liqi
 * 编辑网红平台资料
 */
function editPlat(req, res, next) {
    // 获取参数
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    var cInfoId = retUtil.numberFromReq(req, 'cInfoId') || 0;// 主键ID
    var plat = retUtil.numberFromReq(req,'plat')||0;// 平台
    var platId = retUtil.stringFromReq(req,'platId')||0;// 平台私有ID 例如:抖音ID
    var fansNum = retUtil.numberFromReq(req,'fansNum')||0;// 粉丝数
    var likeNum = retUtil.numberFromReq(req,'likeNum')||0;// 点赞数

    if(!userId || !plat || !platId ||!fansNum || !cInfoId){
        return retUtil.ret_err_json("参数为空",res);
    }

    // 判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0
        ||req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("没有权限",res);
    }

    const currentUserId = req.session[CONST.SESSION_USER].userInfo.userId;

    // 数据请求
    var para = {
        body :{
            userId:userId,
            currentUserId:currentUserId,
            plat:plat,
            platId:platId,
            fansNum:fansNum,
            likeNum:likeNum,
            cInfoId:cInfoId
        },
        method : "POST"
    };

    api4back.editWedRedUserPlat(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/editPlat', editPlat);
router.post('/editPlat', editPlat);


/**
 * 删除网红平台资料
 * @author 李琦
 */
function delUserPlat(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    var info_id = retUtil.numberFromReq(req, 'info_id') || 0;// 平台 info_id

    if(!userId || !info_id){
        return retUtil.ret_err_json("参数为空",res);
    }

    if(req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("浏览器用户失效",res);
    }

    //判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0){
        return retUtil.ret_err_json("没有权限",res);
    }

    //数据请求
    var para = {
        body :{
            info_id:info_id,
            currentUserId:req.session[CONST.SESSION_USER].userInfo.userId,
            userId:userId
        },
        method : "POST"
    };

    api4back.delUserPlat(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/delUserPlat', delUserPlat);
router.post('/delUserPlat', delUserPlat);


/**
 * 编辑网红资料
 * @author 李琦
 */
function editWebRedUser(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId') || 0;// 用户
    var orgId = retUtil.numberFromReq(req, 'orgId') || 0;// 组织ID
    var desc = retUtil.stringFromReq(req, 'desc') || 0;// 用户备注
    var birth_year = retUtil.stringFromReq(req, 'birth_year') || 0;// 出生日期
    var birth_month = retUtil.stringFromReq(req, 'birth_month') || 0;// 出生日期
    var birth_day = retUtil.stringFromReq(req, 'birth_day') || 0;// 出生日期
    var address = retUtil.stringFromReq(req, 'address') || 0;// 地址
    var labs = retUtil.stringFromReq(req, 'labs') || 0;// 标签信息
    var sex = retUtil.numberFromReq(req, 'sex') || 0;// 性别
    var mobile = retUtil.numberFromReq(req, 'mobile') || 0;// 电话号码

    if(!userId || !orgId ||!address){
        return retUtil.ret_err_json("参数为空",res);
    }

    if(req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("浏览器用户失效",res);
    }

    //判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0){
        return retUtil.ret_err_json("没有权限",res);
    }

    address = address.split(',');// 取地址
    //数据请求
    var para = {
        body :{
            userId:userId,
            orgId:orgId,
            currentUserId:req.session[CONST.SESSION_USER].userInfo.userId,
            desc:desc,
            sex:sex,
            birth_year:birth_year,
            birth_month:birth_month,
            birth_day:birth_day,
            prov:address[0],
            city:address[1],
            dist:address[2],
            labs:labs,
            phone:mobile,
        },
        method : "POST"
    };

    api4back.editWebRedUser(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/editWebRedUser', editWebRedUser);
router.post('/editWebRedUser', editWebRedUser);

/**
 * 获取单个平台资料用于修改
 * @author 李琦
 */
function getPlatInfo(req, res, next) {
    var userId = retUtil.numberFromReq(req, 'userId') || 0;
    var cInfoId = retUtil.numberFromReq(req, 'cInfoId') || 0;// 平台主键记录ID

    if(!userId || !cInfoId){
        return retUtil.ret_err_json("所需参数为空",res);
    }

    if(req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("浏览器用户失效",res);
    }

    //判断是否为管理员 如果不是管理员 不能查看用户详情
    if((req.session[CONST.SESSION_USER].orgInfo[0].role & 15)===0){
        return retUtil.ret_err_json("没有权限",res);
    }

    //数据请求
    var para = {
        body :{
            cInfoId:cInfoId,
            currentUserId:req.session[CONST.SESSION_USER].userInfo.userId,
            userId:userId
        },
        method : "POST"
    };

    api4back.getPlatInfo(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/getPlatInfo', getPlatInfo);
router.post('/getPlatInfo', getPlatInfo);


/**
 * 获取合作方(网红公司)参与的活动列表 LQ
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：orgId : 合作方组织id
 * 参数：findStates : 检索条件：活动状态
 * 参数：mobile : 作品作者手机号
 * 参数：actId : 活动ID
 */
function pageGetActPrd(req, res, next) {
    //获取参数
    var pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 10000;
    var withTotalPage = 1;
    var coopOrgId = 0;
    var findStates = retUtil.stringFromReq(req, 'findStates') || "";
    var mobile = retUtil.stringFromReq(req, 'mobile') || "";
    var actId = retUtil.numberFromReq(req, 'actId') || 0;// 活动ID

    if(req.session[CONST.SESSION_USER].userInfo.userId===null){
        return retUtil.ret_err_json("浏览器用户失效",res);
    }

    //参数判断
    if(req.session[CONST.SESSION_USER]
        && req.session[CONST.SESSION_USER].orgInfo[0]
        && req.session[CONST.SESSION_USER].orgInfo[0].orgId){
        coopOrgId = req.session[CONST.SESSION_USER].orgInfo[0].orgId;
    } else {
        return retUtil.ret_err_json("用户组织无效",res);
    }

    if (!actId && actId<=0){
        return retUtil.ret_err_json("活动ID无效",res);
    }

    //数据请求
    var para = {
        body :{
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            orgId:coopOrgId,
            actId:actId
        },
        method : "POST"
    };

    // 作品状态
    if(findStates.length && findStates != "null"){
        para.body.findStates = findStates;
    }
    if(mobile.length && mobile != "null"){
        para.body.mobile = mobile;
    }
    api4back.pageGetActPrd(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        apiRes.pageSize = pageSize;
        if(apiRes.list.length>0){
            for(var i = 0;i<apiRes.list.length;i++){
                if(apiRes.list[i].state == 10){
                    apiRes.list[i].state_desc = "新建";
                }else if(apiRes.list[i].state == 20){
                    apiRes.list[i].state_desc = "审核中";
                }else if(apiRes.list[i].state == 30){
                    apiRes.list[i].state_desc = "审核通过";
                }else if(apiRes.list[i].state == 40){
                    apiRes.list[i].state_desc = "审核不通过";
                }
            }
        };
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/pageGetActPrd', pageGetActPrd);
router.post('/pageGetActPrd', pageGetActPrd);

module.exports = router;
